package com.woniuxy.commons.exception;

import com.woniuxy.commons.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.BindingException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalException {


    @ExceptionHandler(BindingException.class)
    public Result handleBindingException(BindingException e){
        log.error("出现异常",e);
        return Result.getInstance().setCode(500).setMsg(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result handleBindingException(MethodArgumentNotValidException e){
        log.error("出现异常",e);
    return Result.getInstance().setCode(500).setMsg(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }


    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error("出现异常", e);
        return Result.getInstance().setCode(500).setMsg(e.getMessage());
    }

    @ExceptionHandler(MyException.class)
    public Result handleException(MyException e) {
        log.error("出现异常", e);
        return Result.getInstance().setCode(500).setMsg(e.getMessage());
    }
}
